/// @tags: PriorityQueue HeuristicMerge
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

namespace BlueQuantum {

typedef long long LL;

int const N = 2e5 + 5;

int a[N], fa[N], tmp[N], id[N];
int heads[N];
priority_queue<int> pq[N];

struct Edge {
  int v, nxt;
} e[N];

inline void add(int u, int v) {
  static int e_cnt;
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
}

void dfs(int u) {
  for (int i = heads[u], v; i; i = e[i].nxt) {
    dfs(v = e[i].v);
    if (pq[id[u]].size() < pq[id[v]].size()) swap(id[u], id[v]);
    int m = pq[id[v]].size();
    for (int j = 1; j <= m; ++j) {
      tmp[j] = max(pq[id[u]].top(), pq[id[v]].top());
      pq[id[u]].pop(), pq[id[v]].pop();
    }
    for (int i = 1; i <= m; ++i) pq[id[u]].push(tmp[i]);
  }
  pq[id[u]].push(a[u]);
}

inline int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i], id[i] = i;
  for (int i = 2, fa; i <= n; ++i) cin >> fa, add(fa, i);
  dfs(1);
  LL ans = 0;
  while (!pq[id[1]].empty()) ans += pq[id[1]].top(), pq[id[1]].pop();
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P5290 [十二省联考2019] 春节十二响.in", "r", stdin);
  freopen("P5290 [十二省联考2019] 春节十二响.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
